
<template>
  <div class="name1">
    <div class="login_box">
      <!-- 头像 -->
      <div class="login_img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form :rules="loginrule" ref="loginformref" :model="loginform" label-width="0px" class="login_form">
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginform.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginform.password"
            prefix-icon="iconfont icon-3702mima"  show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="loginf">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginform: {
        username: 'admin',
        password: '123456'
      },
      loginrule: {
        username: [
          { required: true, message: '请输入账户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 8, message: '长度在 6 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginf () {
      this.$refs.loginformref.resetFields()
    },
    login () {
      this.$refs.loginformref.validate(async valid => {
        if (!valid) {
          return this.$message.error('错误')
        } else {
          const { data: res } = await this.$http.post('login', this.loginform)
          if (res.meta.status !== 200) {
            return this.$message.error('登录失败')
          } else {
            this.$message.success('登陆成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          }
        }
      })
    }
  }
}
</script>
<style scoped>
.name1 {
  background-color: #2b4b5b;
  width: 100%;
  height: 100%;
}
.login_box{
  width: 450px;
  height: 350px;
  background-color: #fff;
  color: black;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login_img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 1px solid #ccc;
  /* background-color: #fff; */
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login_img img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  position: absolute;
  bottom: 30px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}
.login_form .btns {
  display: flex;
  /* justify-content: flex-end; */
  position: relative;
  right: -260px;
}
</style>
